<template>
  <yd-layout>
    <yd-navbar title="登录">
      <a href="javascript:;" @click="$router.go(-1)" slot="left">
        <yd-navbar-back-icon></yd-navbar-back-icon>
      </a>
    </yd-navbar>
    <yd-cell-group class="demo-small-pitch">
        <yd-cell-item>
            <span slot="left">用户名：</span>
            <yd-input slot="right" required v-model="loginname" max="20" placeholder="请输入用户名"></yd-input>
        </yd-cell-item>
        <yd-cell-item>
            <span slot="left">密码：</span>
            <yd-input slot="right" type="password" v-model="password" placeholder="请输入密码"></yd-input>
        </yd-cell-item>
    </yd-cell-group>
    <yd-button-group>
      <yd-button size="large" type="primary" @click.native="clickLogin">登录</yd-button>
    </yd-button-group>
    <canvas id="qrcode">

    </canvas>
  </yd-layout>
</template>

<script>
import sha1 from 'sha1'

export default {
  name: 'Login',
  data () {
    return {
      loginname: '',
      password: ''
    }
  },
  methods: {
    clickLogin () {
      if (!this.password) {
        this.$dialog.toast({mes: '请输入密码!'})
        return false
      }
      if (!this.loginname) {
        this.$dialog.toast({mes: '请输入用户名!'})
        return false
      }
      let pwd = sha1(this.password)
      this.hdAjax({
        // url: this.API.login,
        url: this.API.wl.mn_login,
        // method: 'post',
        data: {
          password: pwd,
          username: this.loginname
        },
        success: (resultData) => {
          // if (resultData.data && resultData.errcode === 0) {
          if (resultData.status && resultData.status === 1) {
            // 提示信息
            this.$dialog.toast({mes: '登录成功'})
            // 存储用户信息
            let LoginInfo = {}
            LoginInfo.username = this.loginname
            LoginInfo.password = pwd
            this.store.set(this.STORECONST.LOGININFO, LoginInfo)
            let UserInfo = {}
            // UserInfo.accessTicket = resultData.data.accessTicket
            UserInfo.accessTicket = resultData.result
            // UserInfo.userId = resultData.data.userId
            // 有效期(秒)
            // UserInfo.period = resultData.data.period
            UserInfo.period = ''
            let exper = ''
            if (UserInfo.period) {
              exper = new Date().getTime() + (UserInfo.period * 1000)
            }
            if (exper) {
              this.store.set(this.STORECONST.USERINFO, UserInfo, exper)
            } else {
              // 没有有效期 默认半小时
              this.store.set(this.STORECONST.USERINFO, UserInfo, new Date().getTime() + 300000)
            }
            // 登录成功后跳转
            this.$router.push(this.$route.query.redirect || '/')
          } else {
            this.$dialog.toast({mes: resultData.errmsg})
          }
        }
      })
    }
  }
}
</script>


<style>

</style>
